package com.me.test.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.me.test.enums.ErrorCodeEnum;

/**
 * The class Wrap mapper.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Wrapper<T> {

	/** 错误信息 */
	public static final String ERROR_MESSAGE = "fail";

	/** 编号 */
	private String code;

	/** 信息 */
	private String msg;

	/** 结果 */
	private T result;

	/**
	 * Instantiates a new wrapper.
	 */
	public Wrapper(String code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public Wrapper(ErrorCodeEnum err) {
		this.code = err.getCode();
		this.msg = err.getMsg();
	}

	public Wrapper(String code, String msg, T result) {
		this.code = code;
		this.msg = msg;
		this.result = result;
	}

	public Wrapper(ErrorCodeEnum err, T result) {
		this.code = err.getCode();
		this.msg = err.getMsg();
		this.result = result;
	}

	/**
	 * 包装成功结，code=200
	 */
	public static <E> Wrapper<E> ok() {
		return new Wrapper<>(ErrorCodeEnum.OK);
	}

	/**
	 * 包装成功结果
	 */
	public static <E> Wrapper<E> ok(E o) {
		return new Wrapper<>(ErrorCodeEnum.OK, o);
	}

	/**
	 * 包装成功结果
	 */
	public static <E> Wrapper<E> ok(String message, E o) {
		return new Wrapper<>(ErrorCodeEnum.OK.getCode(), message, o);
	}

	/**
	 * 包装失败结果
	 */
	public static <E> Wrapper<E> error(String code, String message) {
		return new Wrapper<>(code, message);
	}

	/**
	 * 包装失败结果
	 */
	public static <E> Wrapper<E> error(ErrorCodeEnum err, String message) {
		return new Wrapper<>(err.getCode(), message);
	}

	public static <E> Wrapper<E> error(ErrorCodeEnum err) {
		return new Wrapper<>(err);
	}

	/**
	 * 判断是否成功： 依据 ErrorCodeEnum.OK.getCode().equals(this.code)
	 *
	 * @return 为 00000,true;否则 false.
	 */
	@JsonIgnore
	public boolean isSuccess() {
		return ErrorCodeEnum.OK.getCode().equals(this.code);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}
}
